/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * CustomerAddEditPnl.java
 *
 * Created on Jul 17, 2011, 11:41:04 AM
 */
package com.viweb.storemanager.ui.customer;

import com.viweb.storemanager.core.CursorManager;
import com.viweb.storemanager.core.MyListCellRenderer;
import com.viweb.storemanager.core.ObjectComboBoxModel;
import com.viweb.storemanager.core.OptionPane;
import com.viweb.storemanager.dao.Biz;
import com.viweb.storemanager.entity.BaseEntity;
import com.viweb.storemanager.entity.Customer;
import com.viweb.storemanager.entity.TypeCustomer;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;

/**
 *
 * @author qhvic
 */
public class CustomerAddEditPnl extends javax.swing.JPanel {

    private CustomerPnl parent;
    private Customer customer;
    private ArrayList<TypeCustomer> typeCustomer;

    /** Creates new form CustomerAddEditPnl */
    public CustomerAddEditPnl() {
        initComponents();
    }

    public CustomerAddEditPnl(CustomerPnl parent) {
        this.typeCustomer = Biz.getInstance().selectAllTypeCustomer();
        initComponents();
        this.parent = parent;
    }

    public void assignDrink(Customer customer) {
        this.customer = customer;

        if (this.customer.getId() != -1) {
            this.cbbTypes.setSelectedIndex(Biz.getObjectIndex((BaseEntity) this.customer.getType(), this.typeCustomer));
            this.txtName.setText(customer.getName());
            this.txtAddress.setText(customer.getAddress());
            this.txtEmail.setText(customer.getEmail());
            this.txtPhone.setText(customer.getPhone());
            this.txtAccountNumber.setText(customer.getAccountNumber());
            this.txtNotes.setText(customer.getNote());
            this.lblDateModifierEdit.setText(customer.getDatemodifies().toString());
            this.txtBank.setText(customer.getBank());
            if (this.customer.getStatus() != 0) {
                this.radioYes.setSelected(true);
            } else {
                this.radioNo.setSelected(true);
            }
        } else {
            this.cbbTypes.setSelectedIndex(-1);
            this.txtName.setText(null);
            this.txtAddress.setText(null);
            this.txtEmail.setText(null);
            this.txtPhone.setText(null);
            this.txtAccountNumber.setText(null);
            this.txtNotes.setText(null);
            this.txtBank.setText(null);
            this.lblDateModifierEdit.setText(null);
        }
    }

    public void setEditableMode(boolean editable) {
        if (editable) {
            this.txtName.requestFocus();
        }
        this.txtName.setEditable(editable);
        this.cbbTypes.setEditable(editable);
        this.txtAddress.setEditable(editable);
        this.txtEmail.setEditable(editable);
        this.txtPhone.setEditable(editable);
        this.txtAccountNumber.setEditable(editable);
        this.txtNotes.setEditable(editable);
        this.txtBank.setEditable(editable);
        this.btnSave.setEnabled(editable);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        java.awt.GridBagConstraints gridBagConstraints;

        btnGroupStatus = new javax.swing.ButtonGroup();
        lblTypes = new javax.swing.JLabel();
        lblNotes = new javax.swing.JLabel();
        lblDateModifier = new javax.swing.JLabel();
        txtName = new javax.swing.JTextField();
        lblEmpty = new javax.swing.JLabel();
        scpNotes = new javax.swing.JScrollPane();
        txtNotes = new javax.swing.JTextPane();
        btnSave = new javax.swing.JButton();
        lblDateModifierEdit = new javax.swing.JLabel();
        lblName = new javax.swing.JLabel();
        cbbTypes = new javax.swing.JComboBox();
        lblAddress = new javax.swing.JLabel();
        lblEmail = new javax.swing.JLabel();
        lblPhone = new javax.swing.JLabel();
        txtAddress = new javax.swing.JTextField();
        txtEmail = new javax.swing.JTextField();
        txtPhone = new javax.swing.JFormattedTextField();
        pnlCustomerInfo = new javax.swing.JPanel();
        lblCustomerInfo = new javax.swing.JLabel();
        sep1 = new javax.swing.JSeparator();
        pnlCustomerDetails = new javax.swing.JPanel();
        lblAccountNumber = new javax.swing.JLabel();
        lblBank = new javax.swing.JLabel();
        pnlTitle = new javax.swing.JPanel();
        lblCustomerDetails = new javax.swing.JLabel();
        sep2 = new javax.swing.JSeparator();
        txtAccountNumber = new javax.swing.JFormattedTextField();
        txtBank = new javax.swing.JTextField();
        lblStatus = new javax.swing.JLabel();
        radioYes = new javax.swing.JRadioButton();
        radioNo = new javax.swing.JRadioButton();

        setLayout(new java.awt.GridBagLayout());

        lblTypes.setText("Loại khách hàng:");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(5, 15, 0, 0);
        add(lblTypes, gridBagConstraints);

        lblNotes.setText("Ghi chú:");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 7;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.insets = new java.awt.Insets(5, 15, 0, 0);
        add(lblNotes, gridBagConstraints);

        lblDateModifier.setText("Ngày cập nhật:");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 8;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(5, 15, 5, 0);
        add(lblDateModifier, gridBagConstraints);

        txtName.setEditable(false);
        txtName.setMinimumSize(new java.awt.Dimension(200, 25));
        txtName.setPreferredSize(new java.awt.Dimension(200, 25));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.gridwidth = 2;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 0, 5);
        add(txtName, gridBagConstraints);

        lblEmpty.setMinimumSize(new java.awt.Dimension(5, 50));
        lblEmpty.setPreferredSize(new java.awt.Dimension(5, 50));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 7;
        gridBagConstraints.gridwidth = 3;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        add(lblEmpty, gridBagConstraints);

        txtNotes.setEditable(false);
        txtNotes.setEnabled(false);
        scpNotes.setViewportView(txtNotes);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 7;
        gridBagConstraints.gridwidth = 3;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.weighty = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 0, 5);
        add(scpNotes, gridBagConstraints);

        btnSave.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/viweb/storemanager/imgae/save.png"))); // NOI18N
        btnSave.setText("Lưu");
        btnSave.setToolTipText("Lưu thể loại vừa thêm mới hay hiệu chỉnh");
        btnSave.setEnabled(false);
        btnSave.setMaximumSize(new java.awt.Dimension(80, 26));
        btnSave.setMinimumSize(new java.awt.Dimension(80, 26));
        btnSave.setPreferredSize(new java.awt.Dimension(80, 26));
        btnSave.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSaveActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 3;
        gridBagConstraints.gridy = 8;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
        add(btnSave, gridBagConstraints);

        lblDateModifierEdit.setMaximumSize(new java.awt.Dimension(200, 14));
        lblDateModifierEdit.setMinimumSize(new java.awt.Dimension(200, 14));
        lblDateModifierEdit.setPreferredSize(new java.awt.Dimension(200, 14));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 8;
        gridBagConstraints.gridwidth = 2;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
        add(lblDateModifierEdit, gridBagConstraints);

        lblName.setText("Tên khách hàng:");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(5, 15, 0, 0);
        add(lblName, gridBagConstraints);

        cbbTypes.setMinimumSize(new java.awt.Dimension(200, 22));
        cbbTypes.setPreferredSize(new java.awt.Dimension(200, 22));
        ObjectComboBoxModel model = new ObjectComboBoxModel(this.typeCustomer);
        cbbTypes.setModel(model);
        cbbTypes.setRenderer(new MyListCellRenderer(22));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.gridwidth = 2;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 0, 5);
        add(cbbTypes, gridBagConstraints);

        lblAddress.setText("Địa chỉ:");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 3;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(5, 15, 0, 0);
        add(lblAddress, gridBagConstraints);

        lblEmail.setText("Email:");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 4;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(5, 15, 0, 0);
        add(lblEmail, gridBagConstraints);

        lblPhone.setText("Số điện thoại:");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 5;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(5, 15, 0, 0);
        add(lblPhone, gridBagConstraints);

        txtAddress.setEditable(false);
        txtAddress.setMinimumSize(new java.awt.Dimension(200, 25));
        txtAddress.setPreferredSize(new java.awt.Dimension(200, 25));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 3;
        gridBagConstraints.gridwidth = 2;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 0, 5);
        add(txtAddress, gridBagConstraints);

        txtEmail.setEditable(false);
        txtEmail.setMinimumSize(new java.awt.Dimension(200, 25));
        txtEmail.setPreferredSize(new java.awt.Dimension(200, 25));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 4;
        gridBagConstraints.gridwidth = 2;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 0, 5);
        add(txtEmail, gridBagConstraints);

        txtPhone.setEditable(false);
        txtPhone.setMinimumSize(new java.awt.Dimension(200, 25));
        txtPhone.setPreferredSize(new java.awt.Dimension(200, 25));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 5;
        gridBagConstraints.gridwidth = 2;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 0, 5);
        add(txtPhone, gridBagConstraints);

        pnlCustomerInfo.setLayout(new java.awt.GridBagLayout());

        lblCustomerInfo.setText("Thông tin khách hàng");
        pnlCustomerInfo.add(lblCustomerInfo, new java.awt.GridBagConstraints());
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(0, 5, 0, 0);
        pnlCustomerInfo.add(sep1, gridBagConstraints);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridwidth = 3;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 0, 5);
        add(pnlCustomerInfo, gridBagConstraints);

        pnlCustomerDetails.setLayout(new java.awt.GridBagLayout());

        lblAccountNumber.setText("Số tài khoản:");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(5, 15, 0, 5);
        pnlCustomerDetails.add(lblAccountNumber, gridBagConstraints);

        lblBank.setText("Ngân hàng:");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(5, 15, 0, 5);
        pnlCustomerDetails.add(lblBank, gridBagConstraints);

        pnlTitle.setLayout(new java.awt.GridBagLayout());

        lblCustomerDetails.setText("Thông tin chi tiết");
        pnlTitle.add(lblCustomerDetails, new java.awt.GridBagConstraints());
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(0, 5, 0, 0);
        pnlTitle.add(sep2, gridBagConstraints);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridwidth = 2;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.insets = new java.awt.Insets(0, 5, 0, 5);
        pnlCustomerDetails.add(pnlTitle, gridBagConstraints);

        txtAccountNumber.setEditable(false);
        txtAccountNumber.setMinimumSize(new java.awt.Dimension(180, 25));
        txtAccountNumber.setPreferredSize(new java.awt.Dimension(180, 25));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 0, 5);
        pnlCustomerDetails.add(txtAccountNumber, gridBagConstraints);

        txtBank.setEditable(false);
        txtBank.setMinimumSize(new java.awt.Dimension(180, 25));
        txtBank.setPreferredSize(new java.awt.Dimension(180, 25));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 0, 5);
        pnlCustomerDetails.add(txtBank, gridBagConstraints);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 3;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.gridheight = 6;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.insets = new java.awt.Insets(5, 20, 30, 5);
        add(pnlCustomerDetails, gridBagConstraints);

        lblStatus.setText("Tình trạng:");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 6;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(5, 15, 0, 0);
        add(lblStatus, gridBagConstraints);

        btnGroupStatus.add(radioYes);
        radioYes.setText("Có");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 6;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 0, 5);
        add(radioYes, gridBagConstraints);

        btnGroupStatus.add(radioNo);
        radioNo.setText("Không");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 2;
        gridBagConstraints.gridy = 6;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(5, 20, 0, 5);
        add(radioNo, gridBagConstraints);
    }// </editor-fold>//GEN-END:initComponents

    private void btnSaveActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSaveActionPerformed
        try {
            // TODO add your handling code here:
            this.customer.setName(this.txtName.getText().trim());
            this.customer.setAddress(this.txtAddress.getText().trim());
            this.customer.setType((TypeCustomer) this.cbbTypes.getSelectedItem());
            this.customer.setPhone(this.txtPhone.getText().trim());
            this.customer.setEmail(this.txtEmail.getText().trim());
            this.customer.setAccountNumber(this.txtAccountNumber.getText().trim());
            this.customer.setNote(this.txtNotes.getText().trim());
            this.customer.setBank(this.txtBank.getText().trim());

            boolean status = false;
            if (this.customer.getId() != -1) {
                status = Biz.getInstance().insertCustomer(customer);
            } else {
                status = Biz.getInstance().updateCustomer(customer);
            }
            // Nếu cập nhật (thêm mới hoặc sữa thông tin) không thành công
            if (status) {
                OptionPane.show(JOptionPane.getFrameForComponent(CustomerAddEditPnl.this), "Cập nhật không thành công! " + Biz.getInstance().getLastError(), "Thông báo", OptionPane.MsgTYPE_WARNING);
                // Nếu cập nhật thành công
            } else {
                if (this.customer.getId() == -1) {
                    // Set id cho quốc gia vừa mới cập nhật (chỉ có ý nghĩa trong trường hợp thêm mới)
                    //this.customer.setId(id);
                    OptionPane.show(JOptionPane.getFrameForComponent(CustomerAddEditPnl.this), "Thêm mới khách hàng thành công!", "Thông báo");
                } else {
                    OptionPane.show(JOptionPane.getFrameForComponent(CustomerAddEditPnl.this), "Cập nhật khách hàng thành công!", "Thông báo");
                }
                this.parent.updateComponentsUI(this.customer);
            }
            CursorManager.setDefaultCursor(this);
        } catch (Exception ex) {
            Logger.getLogger(CustomerAddEditPnl.class.getName()).log(Level.SEVERE, null, ex);
        }
    }//GEN-LAST:event_btnSaveActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.ButtonGroup btnGroupStatus;
    private javax.swing.JButton btnSave;
    private javax.swing.JComboBox cbbTypes;
    private javax.swing.JLabel lblAccountNumber;
    private javax.swing.JLabel lblAddress;
    private javax.swing.JLabel lblBank;
    private javax.swing.JLabel lblCustomerDetails;
    private javax.swing.JLabel lblCustomerInfo;
    private javax.swing.JLabel lblDateModifier;
    private javax.swing.JLabel lblDateModifierEdit;
    private javax.swing.JLabel lblEmail;
    private javax.swing.JLabel lblEmpty;
    private javax.swing.JLabel lblName;
    private javax.swing.JLabel lblNotes;
    private javax.swing.JLabel lblPhone;
    private javax.swing.JLabel lblStatus;
    private javax.swing.JLabel lblTypes;
    private javax.swing.JPanel pnlCustomerDetails;
    private javax.swing.JPanel pnlCustomerInfo;
    private javax.swing.JPanel pnlTitle;
    private javax.swing.JRadioButton radioNo;
    private javax.swing.JRadioButton radioYes;
    private javax.swing.JScrollPane scpNotes;
    private javax.swing.JSeparator sep1;
    private javax.swing.JSeparator sep2;
    private javax.swing.JFormattedTextField txtAccountNumber;
    private javax.swing.JTextField txtAddress;
    private javax.swing.JTextField txtBank;
    private javax.swing.JTextField txtEmail;
    private javax.swing.JTextField txtName;
    private javax.swing.JTextPane txtNotes;
    private javax.swing.JFormattedTextField txtPhone;
    // End of variables declaration//GEN-END:variables
}
